package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //1.保存菜品的基本信息到菜品表中
        this.save(dishDto);
        //2.获取当前保存菜品的id
        Long id = dishDto.getId();
        //3.菜品口味数据处理
        //获取所有的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
//        for (DishFlavor flavor : flavors) {
//            flavor.setDishId(id);
//        }
//        System.out.println(flavors);
        //使用新语法 stream流
        flavors = flavors.stream().map((item)->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        //4.保存口味信息
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //1.先查基本信息
        Dish dish = this.getById(id);
        //2.根据菜品的id查询菜品的口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        //获取当前菜品的口味信息
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        //3.整合菜品信息
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(list);
        //4.返回菜品dto
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //1.修改菜品的基本信息
        this.updateById(dishDto);
        //2.修改口味信息 先把原来的口味信息全部删掉 再添加新的口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        //凡是满足当前查询条件的数据全部删除
        dishFlavorService.remove(lambdaQueryWrapper);
        //添加提交过来的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();    //获取所有的口味信息
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void removeWithFlavor(Long[] ids) {
        //select count(*) from dish where id in (ids) and status=1
        //查询当前删除的菜品状态是否可以删除
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId, ids);
        lambdaQueryWrapper.eq(Dish::getStatus, 1);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0){
            //如果要删除的数组中有启售状态的不能删除 给他抛出一个自定义异常
            throw new CustomException("菜品正在售卖中 不能被删除");
        }
        //如果可以删除 先删除菜品表中的数据
        this.removeByIds(Arrays.asList(ids));
        //delete from dish where dish_id in (ids)
        LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        flavorLambdaQueryWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(flavorLambdaQueryWrapper);
    }
}
